//
//  GFResturtantDetialViewController.m
//  GoodFood
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFResturtantDetialViewController.h"

@interface GFResturtantDetialViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, assign) NSInteger placeId;//传过来的餐厅id
@property (nonatomic, copy) NSString *sessionId;//用户信息
@property (nonatomic, strong) NSMutableArray *arrayForDetial;//承接餐厅详情信息的数组
@property (nonatomic, strong) NSArray *arrayForFood;//推荐菜的数组
@property (nonatomic, strong) NSArray *arrayForComment;//评论的数组
@property (nonatomic, strong) UITableView *tableViewForDetial;//餐厅详细信息的tableView
@end

@implementation GFResturtantDetialViewController
#pragma mark - 导航
- (void)viewForNavi {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, ScreenHeight / 3, ScreenWidth / 3, ScreenHeight / 4)];
    label.text = @"餐馆详情";
    label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    self.view.backgroundColor = [UIColor lightGrayColor];
}
#pragma mark - 初始化
- (instancetype)initWithId:(NSInteger)placeId withSession:(NSString *)sessionId {
    self = [super init];
    if (self) {
        self.placeId = placeId;
        self.sessionId = sessionId;
    }
    return self;
}
#pragma mark - 数据解析
- (void)handleData {
    NSString *url = [NSString stringWithFormat:@"http://api.meishixing.com/place/place/placedetail/place_id=%ld&session_id=%@", self.placeId, self.sessionId];
    
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        
        self.arrayForDetial = [NSMutableArray array];
        NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:result];
        NSDictionary *dic = [dictionary objectForKey:@"result"];
        GFResturtantDetialModel *model = [GFResturtantDetialModel modWithDic:dic];
        [self.arrayForDetial addObject:model];
        self.arrayForComment = [dic objectForKey:@"comment"];
        self.arrayForFood = [dic objectForKey:@"food_list"];
        if (self.arrayForDetial.count != 0) {
            [self.tableViewForDetial reloadData];
        }

        [self.waitView removeFromSuperview];
        [self.overTimeView removeFromSuperview];
    } fail:^(NSError *error) {
        [self.overTimeView removeFromSuperview];
        self.overTimeView = [[GFOverTimeView alloc] initWithFrame:self.view.frame withButtonBlock:^(UIButton *button) {
            [self.view addSubview:self.waitView];
            [self handleData];
        }];
        [self.waitView removeFromSuperview];
        [self.view addSubview:self.overTimeView];
    }];
    
}
#pragma mark - 创建tableView
- (void)creatTableView {

    self.tableViewForDetial = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.tableViewForDetial.delegate = self;
    self.tableViewForDetial.dataSource = self;
    self.tableViewForDetial.bounces = NO;
    self.tableViewForDetial.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableViewForDetial registerClass:[GFResturtantDetialTableViewCell class] forCellReuseIdentifier:@"title"];
    [self.tableViewForDetial registerClass:[GFMapTableViewCell class] forCellReuseIdentifier:@"map"];
    [self.tableViewForDetial registerClass:[GFTagsTableViewCell class] forCellReuseIdentifier:@"tags"];
    [self.tableViewForDetial registerClass:[GFCommentResturtantTableViewCell class] forCellReuseIdentifier:@"comment"];
    [self.tableViewForDetial registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"head"];
    [self.view addSubview:self.tableViewForDetial];
    self.tableViewForDetial.backgroundColor = [UIColor whiteColor];
}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GFResturtantDetialModel *model = [self.arrayForDetial firstObject];
    if (0 == indexPath.section) {
        GFResturtantDetialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"title"];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (1 == indexPath.section) {
        GFMapTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"map"];
        cell.model = model;        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else if (2 == indexPath.section) {
        GFTagsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tags"];
        cell.model = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        GFCommentResturtantTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = model;
        return cell;
    }

}
#pragma mark - row Number
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    GFResturtantDetialModel *model = [self.arrayForDetial firstObject];
//    if (3 == section) {
//        if (model.place_comment_count > 5) {
//            return 5;
//        } else {
//            NSArray *arr = (NSArray *)model.comment;
//            return arr.count;
//        }
//    } else {
//        return 1;
//    }
    return 1;
}
#pragma mark - section Head
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
#pragma mark - section Foot
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
#pragma mark - row Height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GFResturtantDetialModel *model = [self.arrayForDetial firstObject];
    if (0 == indexPath.section) {
        return [[GFResturtantDetialTableViewCell class] heightForCellWithTime:model.open_time withLike:model.favorites_total withLook:model.view_total];
    } else if (1 == indexPath.section) {
        if (model.place_geo_lng == 0 || model.place_geo_lat == 0) {
            return 0;
        } else {
           return ScreenHeight / 3.5;
        }
        
    } else if (2 == indexPath.section) {
        return ScreenHeight / 3;
    }
    else {
        if (model.place_comment_count > 5) {
            return [GFCommentResturtantTableViewCell heightForCellWithModel:model] + ScreenHeight / 15;
        }
        else {
            return [GFCommentResturtantTableViewCell heightForCellWithModel:model];
        }

    }
}
#pragma mark - section Number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.arrayForComment.count == 0) {
        return 3;
    } else {
        return 4;
    }
}
#pragma mark - tableView头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    GFResturtantDetialModel *model = [self.arrayForDetial firstObject];
    if (3 == section) {
        UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
        view.textLabel.text = [NSString stringWithFormat:@"菜品评论(%ld)",model.place_comment_count];
        return view;
    } else {
        return nil;
    }
}
#pragma mark - 视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewForNavi];
    [self handleData];
    [self creatTableView];
    [self.view bringSubviewToFront:self.waitView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
